/*
 * Copyright 2008-2009 Aying.Net Powered by Fuchun.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.aying.html;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.aying.util.IOUtils;
import net.aying.util.html.HtmlRegexpUtils;
import net.aying.util.html.HtmlTags;


/**
 * 
 * 
 * @author  Fuchun
 * @version 1.0, 2009-7-17
 */
public class DefaultHtmlContent implements HtmlContent {
    
    private StringBuffer documentContent;
    
    /**
     * 构造一个新的 {@code DefaultHtmlContent}。
     */
    public DefaultHtmlContent() {
        documentContent = new StringBuffer();
    }
    
    /**
     * 构造一个指定文档内容的 {@code DefaultHtmlContent}。
     * 
     * @param documentBuffer
     */
    public DefaultHtmlContent(StringBuffer documentContent) {
        this.documentContent = documentContent;
    }

    /**
     * @see net.aying.html.HtmlContent#getBody()
     */
    public StringBuffer getBody() {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * @see net.aying.html.HtmlContent#getContentType()
     */
    public String getContentType() {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * @see net.aying.html.HtmlContent#getDoctype()
     */
    public String getDoctype() {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * @see net.aying.html.HtmlContent#getEncoding()
     */
    public String getEncoding() {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * @see net.aying.html.HtmlContent#getHead()
     */
    public String getHead() {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * @see net.aying.html.HtmlContent#getTitle()
     */
    public String getTitle() {
        // TODO Auto-generated method stub
        return null;
    }
    
    /**
     * @see net.aying.html.HtmlContent#getDocumentContent()
     */
    public StringBuffer getDocumentContent() {
        return documentContent;
    }
    
    public static void main(String[] args) throws Exception {
        long start = System.currentTimeMillis();
        URL url = new URL("http://v.youku.com/v_show/id_XMTA1ODI2MjM2.html");
        HttpURLConnection huc = (HttpURLConnection)url.openConnection();
        huc.setConnectTimeout(5000);
        huc.setReadTimeout(10000);
        huc.setDefaultUseCaches(false);
        huc.setRequestMethod("GET");
        
        InputStream input = huc.getInputStream();
        List<String> list = IOUtils.readLines(input, "UTF-8");
        StringBuffer buffer = new StringBuffer();
        for(String line : list) {
            buffer.append(line);
        }
        IOUtils.close(input, true);
        Pattern p = Pattern.compile("\"iku://([^\"]*)\"");
        Matcher m = p.matcher(buffer);
        boolean exists = m.find();
        if(exists) {
            StringBuilder data = new StringBuilder(m.group(1));
            if((int)'|' == (int)data.charAt(0)) {
                data.deleteCharAt(0);
            }
            if((int)'|' == (int)data.charAt(data.length() - 1)) {
                data.deleteCharAt(data.length() - 1);
            }
            String[] datas = data.toString().split("\\|");
            for(String d : datas) {
                if(d.startsWith("%")) {
                    System.out.println(URLDecoder.decode(d, "UTF-8"));
                } else {
                    System.out.println(d);
                }
            }
        }
        System.out.println("请求执行时间： " + (System.currentTimeMillis() - start) + " ms");
    }
}
